const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: '',
    page: 1,
    loadStatus: false,
    not: false
  },
  // 加载更多
  load() {
    let that = this;
    let page = that.data.page;
    if (that.data.loadStatus) { return; }
    if (that.data.not) { return; }
    that.setData({
      loadStatus: true
    })
    page = ++page;
    wx.showLoading({
      title: '加载更多...',
    })
    wx.request({
      url: app.buildUrl('/shop/shop/get-collection'),
      method: 'GET',
      data: {
        page: page,
        status: 0
      },
      header: app.buildHeader(),
      success: function (res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          if (res.data.data.length == 0) {
            app.showToast('没有更多了')
            that.setData({
              not: true
            });
          } else {
            let newList = res.data.data;
            newList.forEach(function (i) {
              if (i.goods.goods_type == 0) {
                i.goods.goods_type = '精品商城';
              } else if (i.goods.goods_type == 2) {
                i.goods.goods_type = '推荐商品';
              } else if (i.goods.goods_type == 3) {
                i.goods.goods_type = '热门商品';
              } else if (i.goods.goods_type == 4) {
                i.goods.goods_type = '新品上线';
              } else if (i.goods.goods_type == 5) {
                i.goods.goods_type = '限时折扣';
              }
              app.transformTime(parseInt(i.create_time) * 1000, options => {
                i.create_time = `${options.Y}-${options.M}-${options.D} ${options.h}:${options.m}:${options.s}`
              })
            })
            that.setData({
              list: that.data.list.concat(newList),
              page: page
            });
          }
        } else {
          app.showToast(res.data.msg)
        }
        that.setData({
          loadStatus: false
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.showLoading({
      title: '加载更多...',
    })
    wx.request({
      url: app.buildUrl('/shop/shop/get-collection'),
      method: 'GET',
      data: {
        page: 1,
        status: 0
      },
      header: app.buildHeader(),
      success: function (res) {
        wx.hideLoading()
        if (res.data.code == 200) {
          let newList = res.data.data;
          newList.forEach(function(i){
            if (i.goods.goods_type == 0){
              i.goods.goods_type = '精品商城';
            } else if (i.goods.goods_type == 2) {
              i.goods.goods_type = '推荐商品';
            } else if (i.goods.goods_type == 3) {
              i.goods.goods_type = '热门商品';
            } else if (i.goods.goods_type == 4) {
              i.goods.goods_type = '新品上线';
            } else if (i.goods.goods_type == 5) {
              i.goods.goods_type = '限时折扣';
            }
            app.transformTime(parseInt(i.create_time) * 1000, options => {
              i.create_time = `${options.Y}-${options.M}-${options.D} ${options.h}:${options.m}:${options.s}`
            })
          })
          that.setData({
            list: newList
          });
        } else {
          app.showToast(res.data.msg)
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})